// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   PostServiceImpl.java

package dale.web.post.service;

import dale.web.category.domain.Category;
import dale.web.category.repository.CategoryRepository;
import dale.web.domain.Condition;
import dale.web.post.domain.Post;
import dale.web.post.domain.PostTag;
import dale.web.post.repository.PostRepository;
import dale.web.post.repository.PostTagRepository;
import dale.web.tag.domain.Tag;
import dale.web.tag.repository.TagRepository;
import java.util.Iterator;
import java.util.List;

// Referenced classes of package dale.web.post.service:
//            PostService

public class PostServiceImpl
    implements PostService
{

    public PostServiceImpl(PostRepository postRepository, CategoryRepository categoryRepository, TagRepository tagRepository, PostTagRepository postTagRepository)
    {
        this.postRepository = postRepository;
        this.categoryRepository = categoryRepository;
        this.tagRepository = tagRepository;
        this.postTagRepository = postTagRepository;
    }

    public List search(Post post, Condition condition)
    {
        return postRepository.list(post, condition);
    }

    public Post detail(int no)
    {
        Post post = postRepository.item(no);
        List tagList = postTagRepository.list(post);
        post.setTagList(tagList);
        return post;
    }

    public void delete(int no)
    {
        postRepository.delete(no);
        postTagRepository.delete(no);
    }

    public int create(Post post)
    {
        int postNo = postRepository.insert(post);
        List tagList = post.getTagList();
        PostTag pt;
        for(Iterator iterator = tagList.iterator(); iterator.hasNext(); postTagRepository.insert(pt))
        {
            Tag t = (Tag)iterator.next();
            String tagID = t.getTagID();
            pt = new PostTag(Integer.valueOf(postNo), tagID);
        }

        return postNo;
    }

    public void modify(Post post)
    {
        if(postRepository.update(post) != 1)
            throw new RuntimeException("modify error");
        int postNo = post.getPostNo();
        postTagRepository.delete(postNo);
        List tagList = post.getTagList();
        PostTag pt;
        for(Iterator iterator = tagList.iterator(); iterator.hasNext(); postTagRepository.insert(pt))
        {
            Tag t = (Tag)iterator.next();
            String tagID = t.getTagID();
            pt = new PostTag(Integer.valueOf(postNo), tagID);
        }

    }

    public int before(int no)
    {
        return postRepository.before(no);
    }

    public int after(int no)
    {
        return postRepository.after(no);
    }

    public int count(Post post, Condition condition)
    {
        return postRepository.count(post, condition);
    }

    public List categoryList(Category category)
    {
        return categoryRepository.list(category);
    }

    public List tagList(Tag tag)
    {
        return tagRepository.list(tag);
    }

    private PostRepository postRepository;
    private CategoryRepository categoryRepository;
    private TagRepository tagRepository;
    private PostTagRepository postTagRepository;
}
